package com.vincent.seeyou.ui;


import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.vincent.seeyou.R;

import java.util.List;

import cn.bmob.im.BmobChatManager;
import cn.bmob.im.bean.BmobChatUser;
import cn.bmob.im.inteface.MsgTag;
import cn.bmob.im.util.BmobLog;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.PushListener;


public class FindFriendsActivity extends BaseAndCheckInActivity {

    /**
     * 有效负荷，目前传递的是username
     */
    private String payLoad;
    private TextView textView_name;

    /* 查询出来的用户*/
    private BmobChatUser bmobChatUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_friends);
        payLoad = synfcManager.handlerIntent(getIntent());
        BmobLog.i(payLoad+"--------------------");
        initView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.find_friends, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        textView_name = (TextView) findViewById(R.id.textView_name);
        if (!payLoad.equals("") && payLoad != null) {
            findFriends();
        } else {
            ShowToast(R.string.tip_error_username);
        }
    }

    private void updateView() {
        StringBuilder builder = new StringBuilder();
        builder.append("userid: ")
                .append(bmobChatUser.getObjectId())
                .append(" ------ ")
                .append("username: ")
                .append(bmobChatUser.getUsername())
                .append(" ------ ")
                .append("nickname: ")
                .append(bmobChatUser.getNick()).toString();

        textView_name.setText(builder);
    }

    private void findFriends() {
        bmobUserManager.queryTargetUserByName(payLoad, new FindListener<BmobChatUser>() {
            @Override
            public void onSuccess(List<BmobChatUser> bmobChatUsers) {

                if (bmobChatUsers != null && bmobChatUsers.size() != 0) {
                    bmobChatUser = bmobChatUsers.get(0);
                    updateView();
                }
            }

            @Override
            public void onError(int i, String s) {

            }
        });

    }
    public void onAdd(View view) {
        final ProgressDialog progress = new ProgressDialog(this);
        progress.setMessage("正在添加...");
        progress.setCanceledOnTouchOutside(false);
        progress.show();
        //发送tag请求  ccc30637bf  kitte bmobChatUser.getObjectId()
        bmobChatManager.sendTagMessage(MsgTag.ADD_CONTACT, bmobChatUser.getObjectId(), new PushListener() {

            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub
                progress.dismiss();
                ShowToast("发送请求成功，等待对方验证!");
            }

            @Override
            public void onFailure(int arg0, final String arg1) {
                // TODO Auto-generated method stub
                progress.dismiss();
                ShowToast("发送请求失败，请重新添加!");
                ShowLog("发送请求失败:" + arg1);
            }
        });

    }

    public void onDisagree(View view) {
    }
}
